//
//  ViewController.swift
//  Design
//
//  Created by john on 12/14/2023.
//  Copyright (c) 2023 john. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    lazy var textView: UITextView = {
        let textView = UITextView()
        textView.isEditable = false
        textView.textContainerInset = .zero
        textView.textContainer.lineFragmentPadding = 0
        let htmlString = "<p>This is <strong>rich</strong> <em>text</em> with <a href=\"https://www.baidu.com\">links iOS遇到问题了</a>.</p>"
        // 将 HTML 字符串转换为富文本
        if let attributedString = try? NSMutableAttributedString(data: Data(htmlString.utf8),
                                                                 options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue],
                                                           documentAttributes: nil) {
            attributedString.yy_color = UIColor.red
            attributedString.yy_font = UIFont.systemFont(ofSize: 20)
            textView.attributedText = attributedString
        }
//        textView.text = htmlString
        return textView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 将 UILabel 添加到视图中
        view.addSubview(textView)
                
        textView.autoPinEdge(toSuperviewEdge: .top, withInset: 50)
        textView.autoPinEdge(toSuperviewEdge: .leading,withInset: 100)
        textView.autoPinEdge(toSuperviewEdge: .trailing, withInset: 100)
        textView.autoPinEdge(toSuperviewEdge: .bottom, withInset: 50)

//
//        let height =  adjustTextViewHeight()
//        textView.autoSetDimension(.height, toSize: height)
    }
    
    func adjustTextViewHeight() -> CGFloat {
         let sizeThatFits = textView.sizeThatFits(CGSize(width: 214, height: CGFloat.greatestFiniteMagnitude))
         return  sizeThatFits.height
     }
}

